package com.usercenter.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.usercenter.entity.AdminProfileExtendEntity;
import com.usercenter.service.AdminProfileService;
import com.adminserver.util.responseUtil.ResponseEnum;
import com.adminserver.util.responseUtil.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

/**
 * 管理员个人中心控制器
 */
@RestController
@RequestMapping("/adminProfile")
public class AdminProfileController {

    @Autowired
    private AdminProfileService adminProfileService;

    /**
     * 获取当前管理员个人资料
     */
    @GetMapping("/getAdminProfile")
    public ResponseUtil getAdminProfile() {
        String account = StpUtil.getLoginId().toString();
        AdminProfileExtendEntity profile = adminProfileService.getProfileByAccount(account);
        
        // 如果没有找到个人资料，则初始化
        if (profile == null) {
            boolean initSuccess = adminProfileService.initProfile(account);
            if (initSuccess) {
                profile = adminProfileService.getProfileByAccount(account);
            } else {
                return ResponseUtil.setE(ResponseEnum.FAIL).setMsg("获取个人资料失败");
            }
        }
        
        return ResponseUtil.setE(ResponseEnum.OK).setData(profile);
    }

    /**
     * 更新管理员个人资料
     */
    @PostMapping("/updateProfile")
    public ResponseUtil updateProfile(@RequestBody AdminProfileExtendEntity profile) {
        String account = StpUtil.getLoginId().toString();
        
        // 设置不可修改字段
        profile.setAccount(account);
        profile.setUpdateTime(new Date());
        
        boolean success = adminProfileService.updateProfile(profile);
        if (success) {
            return ResponseUtil.setE(ResponseEnum.OK).setMsg("个人资料更新成功");
        } else {
            return ResponseUtil.setE(ResponseEnum.FAIL).setMsg("个人资料更新失败");
        }
    }

    /**
     * 上传头像
     */
    @PostMapping("/uploadAvatar")
    public ResponseUtil uploadAvatar(@RequestParam("file") MultipartFile file) {
        String account = StpUtil.getLoginId().toString();
        
        if (file.isEmpty()) {
            return ResponseUtil.setE(ResponseEnum.PARAMS_ERROR).setMsg("上传文件为空");
        }
        
        // 获取文件后缀
        String originalFilename = file.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        
        // 构建保存路径
        String fileName = account + "_" + UUID.randomUUID().toString().replace("-", "") + suffix;
        // 使用项目根目录下的可写目录 uploads/avatars/admin
        File saveDir = new File(System.getProperty("user.dir"), 
                "uploads/avatars/admin");
        if (!saveDir.exists()) {
            saveDir.mkdirs();
        }
        
        try {
            // 保存文件
            File destFile = new File(saveDir, fileName);
            file.transferTo(destFile);
            
            // 更新头像路径
            AdminProfileExtendEntity profile = adminProfileService.getProfileByAccount(account);
            if (profile == null) {
                adminProfileService.initProfile(account);
                profile = adminProfileService.getProfileByAccount(account);
            }
            
            profile.setAvatar("/images/avatars/admin/" + fileName);
            adminProfileService.updateById(profile);
            
            return ResponseUtil.setE(ResponseEnum.OK)
                .setMsg("头像上传成功")
                .setData(profile.getAvatar());
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseUtil.setE(ResponseEnum.FAIL).setMsg("头像上传失败");
        }
    }
    
    /**
     * 获取部门列表
     */
    @GetMapping("/getDepartmentList")
    public ResponseUtil getDepartmentList() {
        return ResponseUtil.setE(ResponseEnum.OK).setData(adminProfileService.getDepartmentList());
    }
} 